const users = require('../mongodb/users')
const mongodb = require('../mongodb/index')

exports.login = function(){
    return (req,res,next)=>{
        mongodb.get(users,{username:req.query.username},data=>{
            if(data.length > 0){
                if(data[0].password == req.query.password){
                    req.session.username = data[0].username;
                    // req.session.userid = data[0].userid;// 有需要可以注释
                    mongodb.set(users,{userid:req.query.userid},{isLogin:1},()=>{
                        res.send(JSON.stringify({
                            code:1,
                            msg:"登录成功"
                        }))
                    })
                }else{
                    res.send(JSON.stringify({
                        code:2,
                        msg:"登录失败，密码不对"
                    }))
                }
            }else{
                res.send(JSON.stringify({
                    code:0,
                    msg:"登录失败，用户名不存在"
                }))
            }
        })
    }
}